package com.liangzhong.manager
{
	import com.common.utils.Reflection;
	
	import flash.media.*;
	import flash.system.*;
	import flash.utils.*;
	/**
	 *声音管理 
	 * @author lxh
	 */	
	public class SoundManager extends Object implements ISoundManager
	{
		private var soundMap:Dictionary;
		private var loopSoundMap:Dictionary;
		private var curMusicName :String = "";
		private var curMusicSound :Sound;
		///data
		private var _musicVolume :Number;
		private var _soundVolume :Number;
		private var _isMusicOpen :Boolean;
		private var _isSoundOpen:Boolean;
		private var lastMusicPlayPos :int;
		private var domain :ApplicationDomain;
		private static var instance:SoundManager;
		
		public function SoundManager()
		{
			soundMap = new Dictionary();
			loopSoundMap = new Dictionary();
			_isSoundOpen = true;
			_isMusicOpen  = true;
			_musicVolume = .2;
			_soundVolume = 1;
		}
		
		public function set isMusicOpen(value :Boolean):void
		{
			_isMusicOpen = value;
			//
			var soundChannel :SoundChannel = loopSoundMap[curMusicName];
			if(soundChannel)
			{
				if(_isMusicOpen)
				{
					if(curMusicSound)
					{
						soundChannel.stop();
						soundChannel = curMusicSound.play(0,10000,soundChannel.soundTransform);
						loopSoundMap[curMusicName] = soundChannel;
					}
				}
				else
				{
					lastMusicPlayPos = soundChannel.position;
					soundChannel.stop();
				}
			}
			else
			{
				if(_isMusicOpen)
				{
					playMusic(curMusicName,true,domain);
				}
			}
		}
		
		public function get isMusicOpen():Boolean
		{
			return _isMusicOpen;
		}
		
		public function set isSoundOpen(value :Boolean):void
		{
			_isSoundOpen = value;
		}
		
		public function get isSoundOpen():Boolean
		{
			return _isSoundOpen;
		}
		
		public function set musicVolume(value :Number):void
		{
			_musicVolume = value;
			//
			if(_musicVolume == 0)
			{
				isMusicOpen = false;
			}
			else
			{
				if(isMusicOpen == false)
				{
					isMusicOpen = true;
				}
			}
			setMusicVolume(curMusicName,_musicVolume);
		}
		
		public function get musicVolume():Number
		{
			return _musicVolume;
		}
		
		public function set soundVolume(value :Number):void
		{
			_soundVolume = value;
			//
			if(_soundVolume == 0)
			{
				isSoundOpen = false;
			}
			else
			{
				_isSoundOpen = true;
			}
			//setSoundVolume(_soundVolume);
		}
		
		public function get soundVolume():Number
		{
			return _soundVolume;
		}
		/**
		 * 
		 * @param name
		 * @param loop
		 * @param appDomain
		 * 
		 */		
		public function playSound(name:String, loop:Boolean = false, appDomain:ApplicationDomain = null) : void
		{
			if (!this.isSoundOpen)
			{
				return;
			}
			var soundInstance:* = Reflection.createInstance(name,appDomain);
			var sound:Sound = soundInstance as Sound;
			if (sound == null)
			{
				return;
			}
			var soundChannel :SoundChannel;
			soundChannel = sound.play(0, 1);
			var transform:SoundTransform = new SoundTransform(_soundVolume);
			if(soundChannel)
			{
				soundChannel.soundTransform = transform;
			}
		}
		/**
		 * 
		 * @param name
		 * 
		 */		
		public function stopSound(name:String) : void
		{
			return;
		}
		/**
		 *play music 
		 * @param name
		 * @param loop
		 * @param appDomain
		 * 
		 */		
		public function playMusic(name:String, loop:Boolean=false, appDomain:ApplicationDomain=null):void
		{
			domain = appDomain;
			curMusicName = name;
			if (!this.isMusicOpen)
			{
				return;
			}
			var soundInstance:* = Reflection.createInstance(name,appDomain);
			var sound:Sound = soundInstance as Sound;
			if (sound == null)
			{
				return;
			}
			stopMusic(name);
			curMusicSound = sound;
			var soundChannel :SoundChannel;
			if(loop)
			{
				soundChannel = sound.play(0, 10000);
			}
			else
			{
				soundChannel = sound.play(0, 1);
			}
			if(soundChannel)
			{
				loopSoundMap[name] = soundChannel;
				var transform:SoundTransform = new SoundTransform(_musicVolume);
				soundChannel.soundTransform = transform;
			}
		}
		/**
		 *stop music 
		 * @param name
		 * 
		 */		
		public function stopMusic(name:String):void
		{
			var soundChannel :SoundChannel = loopSoundMap[name];
			if(soundChannel)
			{
				lastMusicPlayPos = 0;
				soundChannel.stop();
				delete loopSoundMap[name];
			}
		}
		/**
		 * set music volume
		 * @param value
		 * 
		 */		
		public function setMusicVolume(name:String,value:Number = 1):void
		{
			var soundChannel :SoundChannel = loopSoundMap[name];
			if(soundChannel)
			{
				var transform:SoundTransform = new SoundTransform(value);
				soundChannel.soundTransform = transform;
			}
		}
		/**
		 *set sound volume 
		 * 
		 */		
		private function setSoundVolume(value:Number = 1):void
		{
			SoundMixer.soundTransform = new SoundTransform(value);
		}
		/**
		 * stop all
		 * 
		 */		
		public function stopAll() : void
		{
			this.isSoundOpen = false;
			SoundMixer.stopAll();
			SoundMixer.soundTransform = new SoundTransform(0);
		}
		/**
		 * 
		 * start
		 */		
		public function start() : void
		{
			this.isSoundOpen = true;
			SoundMixer.soundTransform = new SoundTransform(1);
			//
			var soundChannel :SoundChannel;
			if(curMusicSound)
			{
				soundChannel = curMusicSound.play(0,10000);
				loopSoundMap[curMusicName] = soundChannel;
			}
		}
		
		public static function getInstance() : SoundManager
		{
			if (instance == null)
			{
				instance = new SoundManager;
			}
			return instance;
		}
		
	}
}